package com.example.demo.ctrl;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName HelloCtrl
 * @Description TODO
 * @Author 黄鹏祥
 * @Date 2021/2/7 21:52
 * @Version 1.0
 **/
@RestController
public class HelloCtrl {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @RequestMapping("/hello")
    public String hello() {
        return "hello haixi.";
    }

    @GetMapping("/hello1")
    public String hello1() {
        final String key = "count";
        final int count;
        if (this.redisTemplate.hasKey(key)) {
            count = Integer.valueOf(this.redisTemplate.opsForValue().get(key)) + 1;
        } else {
            count = 1;
        }
        this.redisTemplate.opsForValue().set(key, count + "");
        return "hello haixi.<br/>这是您第" + count + "次访问。";
    }

}